package persistentie;

import java.util.ArrayList;
import javax.swing.JOptionPane;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import domein.Kamer;
import java.sql.PreparedStatement;
import java.sql.Statement;
public class KamerMapper {

	private final static String LEES_KAMER_SQL = "SELECT tblKamers.* FROM tblKamers;";

	public List<Kamer> geefKamers() {
		List<Kamer> kamerLijst = new ArrayList<Kamer>();

		// create Statement for querying database
		Statement statement;
		Connection connection = PersistentieController.getInstance()
				.getConnection();
		try {
			statement = connection.createStatement();

			// query database
			ResultSet resultSet = statement.executeQuery(LEES_KAMER_SQL);

			while (resultSet.next()) {
				int id = resultSet.getInt("Kamer_id");
				String nummer = resultSet.getString("Kamer_nummer");
				int type = resultSet.getInt("Kamer_type");
				boolean zeezicht = resultSet.getBoolean("Kamer_zeezicht");

				Kamer kamer = new Kamer(id,nummer,type,zeezicht);
				kamerLijst.add(kamer);
				System.out.println("Kamer met ID:" + id + " opgehaald.");
			}
			statement.close();
			return kamerLijst;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
//wijzigen Kamer - WIP
	private PreparedStatement initWijzigKamer(Connection connection) {
		try {
			PreparedStatement pStatement = connection
					.prepareStatement(UPDATE_SQL); // HERSCHRIJVEN
			return pStatement;
		}
		// detect problems interacting with the database
		catch (SQLException sqlException) {
			JOptionPane.showMessageDialog(null, sqlException.getMessage(),
					"Database Error", JOptionPane.ERROR_MESSAGE);
			System.exit(1);
		}
		return null;
	}
//Klant wijzigen - WIP
	public void wijzigKamer(Kamer kamer) {
		Connection connection = PersistentieController.getInstance()
				.getConnection();
		PreparedStatement pStatement = initWijzigKamer(connection);

		try {
			pStatement.setString(3, kamer.getNummer());
			pStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}
}